package com.example.sourcecodelearning.controller;

import com.example.framework.common.pojo.R;
import com.example.framework.operatelog.core.annotation.OperationLog;
import com.example.sourcecodelearning.modules.file.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "文件接口")
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileUploadService fileUploadService;

    @PreAuthorize("hasAuthority('system:menu:edit')")
    @OperationLog("上传文件")
    @Operation(summary = "上传文件")
    @PostMapping(value = "upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Long> upload(@RequestPart MultipartFile file) {
        Long fileId = fileUploadService.upload(file);
        return R.success(fileId);
    }
}
